package com.usefullc.system.configuration.jackson;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.core.convert.converter.Converter;

import java.io.IOException;

@JsonComponent
public class BooleanDeserializer extends JsonDeserializer<Boolean> implements Converter<String, Boolean> {

    @Override
    public Boolean convert(String source) {
        if(StringUtils.equalsIgnoreCase(source,"1")){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        return convert(p.getValueAsString());
    }
}
